package com.qfedu.yanglao20admin.plancase.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.yanglao20.common.CommonResult;
import com.qfedu.yanglao20.common.annotation.LogInfo;
import com.qfedu.yanglao20.common.utils.OssUtils;
import com.qfedu.yanglao20.entity.plancase.CaseList;
import com.qfedu.yanglao20admin.plancase.annotation.OperateMethod;
import com.qfedu.yanglao20admin.plancase.param.CaseListParam;
import com.qfedu.yanglao20admin.plancase.param.CaseListVOParam;
import com.qfedu.yanglao20admin.plancase.service.CaseListService;
import com.qfedu.yanglao20admin.plancase.vo.CaseListVO;
import com.qfedu.yanglao20admin.plancase.vo.LookCaseListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * @author yujie
 * @data 2023/4/23 18:09
 */
@Api(tags = "预案列表")
@RestController
@RequestMapping("/plan")
public class CaseListController {

    @Resource
    private CaseListService caseListService;

    @LogInfo
    @ApiOperation(value = "分页展示预案列表信息", notes = "分页展示预案列表信息")
    @GetMapping("/page")
    public CommonResult<CaseListVO> page(CaseListParam param) {
        Page<CaseListVO> caseListVOPage = caseListService.pageCaseList(param);
        return CommonResult.success(caseListVOPage);
    }

    @LogInfo
    @ApiOperation(value = "根据id删除预案列表信息", notes = "根据id删除预案列表信息")
    @GetMapping("/delete")
    public CommonResult<CaseList> deleteCaseList(@RequestParam @ApiParam("预案列表id") Integer id) {
        caseListService.deleteById(id);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "根据id查询现场处置预案信息", notes = "根据id查询现场处置预案信息")
    @GetMapping("/query")
    public CommonResult<LookCaseListVO> queryCaseList(@RequestParam @ApiParam("预案类型id") Integer id) {
        LookCaseListVO lookCaseListVO = caseListService.selectCaseVOById(id);
        return CommonResult.success(lookCaseListVO);
    }

    @LogInfo
    @ApiOperation(value = "添加现场处置预案以及预案指令", notes = "添加现场处置预案以及预案指令")
    @PostMapping("/add")
    public CommonResult<CaseListVOParam> addCaseList(@RequestBody CaseListVOParam caseListVOParam) {
        caseListService.insertCaseList(caseListVOParam);
        return CommonResult.success();
    }

    @LogInfo
    @OperateMethod
    @ApiOperation(value = "修改处置预案以及预案指令", notes = "修改处置预案以及预案指令")
    @PostMapping("/update")
    public CommonResult<CaseListVO> updateCaseAffiliation(@RequestBody CaseListVOParam caseListVOParam) {
        caseListService.updateCaseList(caseListVOParam);
        return CommonResult.success();
    }

    @LogInfo
    @ApiOperation(value = "上传附件", notes = "上传附件")
    @PostMapping("/upload")
    public CommonResult<CaseList> upload(MultipartFile file) {

        String path = null;

        String name = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replace("-", "");
        name = uuid + name.substring(name.lastIndexOf("."));

        try {
            path = OssUtils.uploadFile2OSS(file.getInputStream(), name);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.success(path);
    }
}
